<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- el-card：带了box-shadow的div -->
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <!-- 左侧占据20份 -->
          <el-col :span="20">
            <span>{{ company.name
            }}</span>
          </el-col>
          <!-- 右侧占据4份 -->
          <el-col :span="4">
            <el-row type="flex">
              <!-- 左右侧各占一份 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 作用域插槽获取每行部门对象，让组件结构支持自定义，data就是每行的部门对象 -->
        <el-tree :data="Treedata" :props="{label:'name'}">
          <!-- 用了一个行列布局 -->
          <!-- 作用域插槽获取每行部门对象，让组件的结构支持自定义 -->
          <template #default="{data}">
            <tree-tool
              :item="data"
              @del-dept="handleDelDept"
              @show-dept="handleshowAddDepts"
              @edit-dept="handleshowEditDepts"
            />

          </template>
        </el-tree>
        <add-dept :show-dialog.sync="showDialog" :dept-list="deptList" :item="deptObj" @add-dept="getDepListAPI" />
        <edit-dept :show-dialog.sync="showlog" :dept-list="deptList" :item="deptObj" @edit-dept-success="getDepListAPI" />
      </el-card>

    </div>
  </div>
</template>

<script>
import { depListAPI, DelDepartments } from '@/api/departments'
import { list2Tree } from '@/utils/shift'
import TreeTool from './components/tree-tool.vue'
import AddDept from './components/add-dept.vue'
import EditDept from './components/edit-dept.vue'
export default {
  name: 'DepartmentsView',
  components: {
    TreeTool,
    AddDept,
    EditDept
  },
  data() {
    return {
      // 部门列表
      deptList: [],
      // 部门树形数据
      Treedata: [],
      // 公司信息
      company: {
        name: '',
        manager: '',
        id: ''
      },
      showDialog: false,
      showlog: false,
      // 记录当前操作的部门对象
      deptObj: {}
    }
  },
  created() {
    this.getDepListAPI()
  },
  methods: {
    // 获取部门列表
    async getDepListAPI() {
      // 调接口发请求
      this.loading = true
      const resp = await depListAPI()
      // console.log(resp)
      const { data } = resp
      this.company.name = data.companyName
      this.company.manager = data.companyManage
      this.company.id = data.companyId
      // 这里不能直接赋值，因为depts不符合树形结构
      // 调用list2Tree函数，把depts部门列表展示过去
      this.Treedata = list2Tree(data.depts, '')
      // 保存一份列表数据
      this.deptList = data.depts
      this.loading = false
    },
    // 删除部门
    async handleDelDept(deptId) {
      await this.$confirm('你确定要删除嘛？', '温馨提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(async() => {
        await DelDepartments(deptId)
        this.getDepListAPI()
        this.$message.success('删除成功')
      }).catch((err) => {
        console.log(err)
      })
    },
    // 处理添加操作, 显示弹层
    handleshowAddDepts(deptObj) {
      // 显示弹层
      this.showDialog = true
      // 给正在操作的部门对象赋值
      this.deptObj = deptObj
    },
    handleshowEditDepts(deptObj) {
      // 显示弹层
      this.showlog = true
      // 给正在操作的部门对象赋值
      this.deptObj = deptObj
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  // 样式穿透，加scoped的时候需要穿透
  ::v-deep {

    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }

    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }

    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }

    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
